﻿using System.Collections.Generic;
using System.Xml;

using Sunyi.Project.WebPoint.Common;

namespace Sunyi.Project.WebPoint.Configuration
{
    public sealed class CommonConfiguration
    {
        private Dictionary<string, string> items = new Dictionary<string, string>();

        public string this[string key]
        {
            get
            {
                if (items.ContainsKey(key)) return items[key];

                return null;
            }
        }

        public CommonConfiguration(XmlNode node)
        {
            foreach (XmlNode subNode in node.ChildNodes)
            {
                if (subNode.NodeType != XmlNodeType.Element) continue;

                string key = subNode.Name;
                string value = null;

                if (subNode.Attributes[WebPointGlobalConstant.Value] != null)
                {
                    value = subNode.Attributes[WebPointGlobalConstant.Value].Value;
                }
                else if (subNode.Attributes[WebPointGlobalConstant.Value.ToLower()] != null)
                {
                    value = subNode.Attributes[WebPointGlobalConstant.Value.ToLower()].Value;
                }

                if (!items.ContainsKey(key))
                {
                    items.Add(key, value);
                }
            }
        }
    }
}
